package com.ykdl.member.kid.messagebox;

import java.util.HashMap;
import java.util.Map;

import oauth.signpost.http.HttpParameters;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.PraisesMeAdapters;
import com.ykdl.member.kid.beans.FeedFriendsListBean;
import com.ykdl.member.kid.beans.PraiseBean;
import com.ykdl.member.kid.beans.PraiseListBean;
import com.ykdl.member.kid.beans.SystemMessageListBean;
import com.ykdl.member.kid.beans.TopicBean;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.messagebox.MyMessageAcivity1.getSystemMessageTag;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class PraisesMeListActivity extends BaseScreen{
	private PraisesMeAdapters adapter;
	private RefreshListView praises_list;
	private String TAG = "PraisesMeListActivity";
	private TextView praises_num ;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 30;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.praisesmelistactivity_xml);
		inint();
		}
	public void inint() {
		adapter = new PraisesMeAdapters(this,TAG);
		findViewById(R.id.main_right).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PraisesMeListActivity.this.finish();
			}
		});
		praises_list = (RefreshListView)findViewById(R.id.praises_list);
		praises_num = (TextView)findViewById(R.id.praises_num);
		praises_list.setMoreClick(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getPraises() ;
			}
		});
		praises_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				PraiseBean bean = (PraiseBean)adapter.getItem(arg2);
				Intent intent = new Intent(mContext, TopicAndResultActivity.class);
				intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, bean.getPost().getTopic_id()+"");
				intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, bean.getPost().getGroup().getGroup_id()+"");
				startActivity(intent);
			}
		});
		getPraises() ;
		deltMyPraiseList();
	}
	public void deltMyPraiseList(){
		String pageurl=KidConfig.DELT_MY_PRAISES;
		HttpParameters bodyParams = new HttpParameters();
		HttpBaseRequest request = Wxxr.getInstance().deltteRequest(pageurl, bodyParams);
		TaskManager.startHttpRequest(request, null, null);
	}
	public void getPraises() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.GET_MY_PRAISES;
		Map<String, String> map = new HashMap<String, String>();
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,"");
		 TaskManager.startHttpRequest(request, new getPraisesTag(),  PraiseListBean.class);
	}
	
	class getPraisesTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载成功", 1).show();
			if (t instanceof PraiseListBean) {
				PraiseListBean bean = (PraiseListBean)t;
				if (bean.getList()!=null) {
					praises_num.setText(bean.getList().size()+"");
				}else {
					praises_num.setText("");
				}
				if (bean.getNext_cursor() == bean.getTotal_number()) {
					praises_list.setMoreButtoIsGon(true);
				} else {
					praises_list.setMoreButtoIsGon(false);
					cursor = bean.getNext_cursor();
				}
				adapter.setlist(bean.getList());
				praises_list.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
}
